package com.pyb.config;
import com.pyb.Interceptor.Interceptor1;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Spring MVC相关配置 (替换掉了springmvc.xml)
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.pyb.controller","com.pyb.config"},
        includeFilters = @ComponentScan.Filter(type= FilterType.ANNOTATION, classes = org.springframework.stereotype.Controller.class))
public class SpringMVCConfig implements WebMvcConfigurer {

    /**
     * 添加视图控制器，如果没有指定，就默认访问这个界面
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("epidemic"); // 访问的是epidemic.jsp
    }

    /**
     * 配置视图解析器（前后缀）
     */
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    /**
     * 配置日期转换器
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter<String, Date>() {
            private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            @Override
            public Date convert(String source) {
                try {
                    return dateFormat.parse(source);
                } catch (ParseException e) {
                    throw new IllegalArgumentException("无法解析日期：" + source, e);
                }
            }
        });
    }
    /**
     * 如果没有匹配到action，则使用servlet默认的访问
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册自定义的拦截器，并指定要应用的路径模式和排除的路径模式。
        registry.addInterceptor(new Interceptor1())
                .addPathPatterns("/**") // 应用到所有路径
                .excludePathPatterns("/users", "/register","/login","/accounts","/simpleType"); // 但排除登录和注册页面
    }
    @Bean
    public MethodValidationPostProcessor validationPostProcessor() {
        return new MethodValidationPostProcessor();
    }

}